package com.lzxuni.modules.weather.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.weather.entity.WeatherLimit;
import com.lzxuni.modules.weather.entity.WeatherLimitDetail;
import com.lzxuni.modules.weather.service.WeatherLimitDetailService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.weather.service.WeatherLimitService;
import org.springframework.web.bind.annotation.RestController;
import com.lzxuni.modules.common.controller.BaseController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 天气信息_天气管理_临界值 前端控制器
 * </p>
 *
 * @author LaoNa
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/Weather/Limit")
public class WeatherLimitController extends BaseController {

    @Autowired
    private WeatherLimitService weatherLimitService;

    @Autowired
    private WeatherLimitDetailService weatherLimitDetailService;
    private String templatesPath = "/Weather/Limit";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView(templatesPath + "/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,   WeatherLimit weatherLimit) {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);

        PageData pageData = getPageData(weatherLimitService.queryPage(pageParameter, weatherLimit));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String type,String id) {
        ModelAndView mv = new ModelAndView(templatesPath + "/form");
        if(StringUtils.isNotBlank(type)){
            if(StringUtils.isNotBlank(id)){
            List<WeatherLimitDetail> list=weatherLimitDetailService.list(new QueryWrapper<WeatherLimitDetail>().eq("limit_id",id).orderByAsc("create_date"));
                if(type.equals("edit")||type.equals("query")){
                    mv.addObject("detailList",list);
                    mv.addObject("detailCoount",list.size());

                }
            }
            mv.addObject("type",type);
        }
        return mv;
    }

    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        weatherLimitService.removeById(keyValue);
        weatherLimitDetailService.remove(new QueryWrapper<WeatherLimitDetail>().eq("limit_id",keyValue));
        return R.ok("删除成功");
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, WeatherLimit weatherLimit, String subJson) {
        weatherLimit.setLimitId(keyValue);
        weatherLimit.setCropId(weatherLimit.getCropModel());
        System.out.println(weatherLimit.toString());
        List<WeatherLimitDetail> weatherLimitDetailList = JSON.parseArray(subJson, WeatherLimitDetail.class);
        String id = "";
        if (StringUtils.isEmpty(weatherLimit.getLimitId())) {
            id = UuidUtil.get32UUID();
            weatherLimit.setLimitId(id);
        } else {
            id = weatherLimit.getLimitId();
        }
        if (weatherLimitDetailList != null && weatherLimitDetailList.size() > 0) {
            for (WeatherLimitDetail weatherLimitDetail : weatherLimitDetailList) {
                weatherLimitDetail.setLimitId(id);
            }
        }
        if (StringUtils.isEmpty(keyValue)) {

            weatherLimitService.save(weatherLimit);
            weatherLimitDetailService.saveBatch(weatherLimitDetailList);
            return R.ok("保存成功");
        } else {
            weatherLimitDetailService.remove(new QueryWrapper<WeatherLimitDetail>().eq("limit_id",id));
            weatherLimitService.updateById(weatherLimit);
            weatherLimitDetailService.saveBatch(weatherLimitDetailList);
            return R.ok("修改成功");
        }
    }


}

